tip_for_Taya='2';



f1=fopen('from_excel.txt','r');
n=516;

y=zeros(1,n);
for i=1:n
y(1,i)=length(fgetl(f1));
end;
fclose(f1);

f1=fopen('from_excel.txt','r');

ssname={'tst' 'tt'};
sstype={'dd' 'as'};
j=0;
for i=1:n

    if (y(1,i)>48)
        
    j=j+1
    ssname(j)=cellstr(fscanf(f1,'%s',1));
    ssname(j);
    fscanf(f1,'%s',4);
    sstype(j)=cellstr(fscanf(f1,'%s',1));
    else
        'look';
        i;
        j;
    end;
    
    fgetl(f1);
    
end;

fclose(f1);


f1=fopen ('fps_and_times.txt','r');

spapname={'aaa' 'bbbbb'};
fpsandtimes=zeros(33,3);
for i=1:33
    
     spapname(i)=cellstr(fscanf(f1,'%s',1));
     fpsandtimes(i,:)=fscanf(f1,'%g',3);
end;

fclose(f1);


spapname(10)

fpsandtimes(30,:)


j;


nexcel=j;






n=100;                        % initial number of data structures ( platelets)

N1=25;

N2=7;


field1='global_parameters'
field2='test';

array8(n)=struct(field1, [], field2, []); 


s=dir;                         % current dirrectory path

jtrue=1;




f47=fopen('badnames.txt','w');



f22=fopen('plt_new.txt','w');

for pap=3:35                     % cycle of folder scanning (pap=folder number)

s2=s(pap).name;


i=1;
j=1;

s5=[pwd,'\',s2,'\'];


while (i<50)                   % cycle of running through the given folder - analyzing 50 possible filenames 
s6=[num2str(i),'_plt','\'];

s7=[s5,s6];

s8=[s2,'_',num2str(j),'.txt'];

s9=[s7,s8];

s9;

f1=fopen(s9,'r');

if (f1==-1)
    j=j+1;
    if (j>50)
        i=i+1;
        j=1;
    end;
    
else
 
  kotcheck=0;
  
for kot=1:33
    stes=char(spapname(kot));
    
      if (length(s2)==length(stes))
          if (s2==stes)
              array8(jtrue).fps=fpsandtimes(kot,1);
              array8(jtrue).start=fpsandtimes(kot,2);
               array8(jtrue).end=fpsandtimes(kot,3);
               kotcheck=1;
          end;
      end;
end;

    if (kotcheck==0)
        fprintf (f47,'%s \n',s2);
    end;
    
    
sname=[s2,'_plt_',num2str(i),'.txt']; 
   
array8(jtrue).name=sname;

if (length(s2)==35)    
    array8(jtrue).shear=1000;
else                                     % !!!!!!!!!!!!!! shear rate !
    array8(jtrue).shear=100;
end;


                                                % !!!!!!!!!!!!!!!!!!!!!

 
a=fileread(s9);                                   
if (length(a)<100)
    array8(jtrue).datatype=0; 
    a;% no clear first release
else
    a;
    array8(jtrue).datatype=1;                       % clear first release
    


    
    
    
    
    s10=[s7,'spike_data_random_secretion_',num2str(j),'.txt'];     % reading data on random secretion statistics
    f3=fopen (s10,'r');
    
    count=0;
    masdat=zeros(100,9);
    
    for cntt=1:100
    
  s77=fscanf (f3,'%s',1);                      % first value (random secretion time) will be ignored 
  
  if (length(str2num(s77)>0))
      count=count+1;
      masdat(count,1)=str2num(s77);
      for taya=2:9
      masdat(count,taya)=fscanf (f3,'%g',1);
      end;
  else
      fscanf (f3,'%s',4);
  end;
  
  
      
    end;
    
    
    for cntt=count+1:100                         % in case if no spike before random release is detected, the delay is set to 10000
        masdat(cntt,3)=10000;
    end;
    
  
      array8(jtrue).datrand=masdat;             % !!!!!!!!!!!!!!!!!!!!!!
    
    fclose(f3);
    
end;





if (array8(jtrue).datatype==1)
    N=N1;
    
else
    N=N2;

end;
    

curr=zeros(1,N);


     
for k=1:N
    
    if (k==3)
     tes=fscanf (f1,'%s',1);
     if (tes(1)=='N')
         curr(1,k)=0;
        'really?????'
    
  
    else
    curr(1,k)=str2num(tes);
    end;
    
    else
        tes=fscanf (f1,'%g',1);
        curr(1,k)=tes;
    end;
end;

  if N==N2
for k=1:N
    fprintf (f22,' %g',curr(1,k));
end;
fprintf (f22,'\n');
  end;

  
  
array8(jtrue).data=curr;                     % !!!!!!!!!!!!!!!!!!!!!!!!!
    




                    % reading experimental data on calcium spikes  !!!!!!!!!!!!!!
                                                

s10=[s7,'spikes_data_new_',num2str(j),'.txt'];
    f3=fopen(s10,'r');
    caheader=zeros(1,6);
    caspikedata=zeros(1,4);
    
    for u=1:6
        caheader(1,u)=fscanf (f3,'%g',1);
    end;
    u=0;
    
   if (curr(1,3)>0)
    while (feof(f3)==0)
        u;
        u=u+1;
        caspikedata(u,:)=fscanf(f3,'%g',4);
        fgetl(f3);
        
    end;
   end;
   
   
    array8(jtrue).caspikes=caspikedata;     % !!!!!!!!!!!!!!!!!!!!!!!!
    array8(jtrue).caheader=caheader;
    
    fclose(f3);



     

    jtrue=jtrue+1;
    j=1;
    i=i+1; 
    
    fclose(f1);
    
end;




end;

jtrue;
end;


fclose (f22);

jtrue-1



chha=0;

scurrent='asd';

chha2=0;

'start'


for i=1:jtrue-1
    
    chha2=0;
    
    for j=1:nexcel
        
        scurrent=char(ssname(j));
        
        if (length(array8(i).name)==length(scurrent))
            
             if (array8(i).name==scurrent)
                 array8(i).sstype=char(sstype(j));
                 chha=chha+1;
                 chha2=chha2+1;
                 
                  if (chha2>1)
                      scurrent
                  end;
                  
                  
             else
                'not found';
               
             end;
          end;
        end;
        
       
    if chha2==0
        array8(i).name
    end;
end;





chha
i






 % !!!!!!!!!!!!!!!!!!!!!!!!!!!!    ANALYSIS            !!!!!!!!!!!!!!!!!!
 
k=0;

X=zeros(3,N1);

for i=1:jtrue-1
    i;
   if (array8(i).datatype==1)
       
       k=k+1;
       X(k,:)=array8(i).data;                % !!!!!!!!!!!!!! 
       
   end;
   
end;

'with datatype 1'
k

mas=zeros(k,N1);
masrand=zeros(k,100);

k=0;

kk=0;

for i=1:jtrue-1
    
     if (array8(i).datatype==1)
       
       k=k+1;
       
       mas(k,:)=array8(i).data;
       masrand(k,:)=array8(i).datrand(:,3)';
       
%        if (k==5)
%            array8(i).name
%        end;
       
   end;
   
end;

% 
% 
%    f=figure;
%    scur=['Delay time Vs Time of release start'];
%    set (f,'name',scur);
%    figure(f);
%    scatter (mas(:,21),mas(:,4));
%    
   
   z=0:0.5:50;
   
   y=zeros(1,length(z));
   
   for i=1:k
       
   y0=histc(masrand(i,:),z);
   y=y+y0;
   end;
   
%    f=figure;
%    figure(f);
%    scur=['Delay time experimental and simulated'];
%    set (f,'name',scur);
%    a=axes;
%    axes(a);
%    
%    plot (z,y);
%    set (a,'nextplot','add');
%    
    yexp=histc(mas(:,21)',z);
%    h=plot (z,yexp);
%    set (h,'color','red');
      
   f18=fopen('delay_time.txt','w');
   
   for i=1:length(z)
     fprintf (f18,'%g %g \n',y(1,i), yexp(1,i));
   end;
   
   fclose (f18);
   
jtrue-1

% array8(jtrue-1).name
% array8(jtrue-1).caheader
% array8(jtrue-1).caspikes
% array8(jtrue-1).datatype

numspik=zeros(1,jtrue-1);
numspikbef=numspik;

type=zeros(1,jtrue-1);
shear=zeros(1,jtrue-1);
timeoflastspik=zeros(1,jtrue-1);


for i=1:jtrue-1
    [hei,wid]=size(array8(i).caspikes);
    
    bef=0;
    
    for j=1:hei
        if (array8(i).caspikes(j,2)<0)
            bef=bef+1;
        end;
    end;
    numspik(1,i)=hei;
    numspikbef(1,i)=bef;
    timeoflastspik(1,i)=array8(i).caspikes(j,2);
    type(1,i)=array8(i).datatype;
    shear(1,i)=array8(i).shear;
end;

% f=figure;
%    scur=['Type of platelet (0 - others, 1 - clear first release Vs Number of peaks'];
%    set (f,'name',scur);
% figure(f);
% a=axes;
% axes(a);
% scatter (type,numspik);
% 
% set (a,'xlim',[-1 2]);

   f19=fopen('type_of_plt_with_number_of_peaks.txt','w');
   
   for i=1:jtrue-1
     fprintf (f19,'%s %g %s %g %g %g %g\n',array8(i).name,type(1,i),array8(i).sstype, numspik(1,i),numspik(1,i)-numspikbef(1,i),timeoflastspik(1,i),60*(numspik(1,i)-numspikbef(1,i))/array8(i).end );
   
   array8(i).freq=60*(numspik(1,i)-numspikbef(1,i))/array8(i).end;
   
   end;
   
   fclose (f19);

%     
% f=figure;
%    scur=['Type of platelet (0 - others, 1 - clear first release Vs Number of peaks before activation'];
%    set (f,'name',scur);
% figure(f);
% a=axes;
% axes(a);
% scatter (type,numspikbef);
% 
% set (a,'xlim',[-1 2]);



% f=figure;
% figure(f);
% 
% plot (numspikbef);


z=0:20;
% 
% y=histc(numspikbef, z);
% 
% f=figure;
% figure(f);
%    scur=['Number of peaks before activation histogram'];
%    set (f,'name',scur);
% plot (z,y);
% 
% s2
% length(s2)
% 
% 
% f=figure;
%    scur=['Shear (100 or 1000s) Vs Number of peaks'];
%    set (f,'name',scur);
% figure(f);
% 
% scatter (shear,numspik);

   f21=fopen('shear_stress_with_number_of_peaks.txt','w');
   
   for i=1:jtrue-1
     fprintf (f21,'%g %g \n',shear(1,i), numspik(1,i));
   end;
   
   fclose (f21);

%    
%    
% f=figure;
%    scur=['Correlation matrix of platelets which have clear first release'];
%    set (f,'name',scur);
% figure(f);
% 
 ycor=corr(X);
%                          % !!!!! correlation matrix  
% imagesc (ycor);

%colormap (gray);


for i=1:N1-1
    for j=i+1:N1
        
        
 if (ycor(i,j)>0.8) 
     
     i;
     j;
     ycor(i,j);
 end;
 
    end;
end;

% 
% f=figure;
%    scur=['Correlation of time of first release start with other parameters'];
%    set (f,'name',scur);
% figure(f);
% 

%%%%%%%%%%%%%%%   checking correlation of given parameter number with
%%%%%%%%%%%%%%%   others

% plot (ycor (4,:));

% 
% f=figure;
%    scur=['Correlation of amplitude of first release Vs Calcium integral at release'];
%    set (f,'name',scur);
% figure(f);
% 
% scatter (X(:,6),X(:,15));


k=0;


array8(jtrue-1).name;

fclose (f47);

fpsdat=zeros(1,jtrue-1);
for i=1:jtrue-1
   fpsdat(1,i)= array8(i).fps;
end;


% 
% f=figure;
% figure(f);
% 
% plot (fpsdat);
% 







%  ANALYZ of spike density and other stuff !!!!!!!!!!!!!!!!!!!!!!!






zz77=0:0.01:1;

yfrq=zeros(1,length(zz77));



kkk=0;


zz=0.5:0.5:120;
yy=zeros(1,length(zz));

 

poloknasize=5;        % razmer polovini okna v sekundah !!!!!!!!!!!!!!!

f17=fopen(['new_peak_density_data_new_okno_' num2str(2*poloknasize) '_tip_' tip_for_Taya '.txt'],'w');


f22=fopen(['new_amplitudes_data' '_tip_' tip_for_Taya '.txt'],'w');






maxtime=5;     % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


maxtime2=3;     % max interval for additional interval distribution





mintime=2;

masp4k=zeros(1,100);
p4kcount=0;
strno='no';
strtest='aa';

k=0;
cntshort=0;
statshort=zeros(1,10);

timeinp4k=zeros(1,10);
timeinp4kcount=0;

freqdistrib=0;
z7=0:30;

locfreqsum=zeros(5,1200); 


tip=0;

adp='ADP';

nopestr='no';


thrombin='thr';

curstr='tes';
curtrip='tee';

curtripconc='000';

highconc='0.1';


countadp=0;
countthr=0;
counthigh=0;

mastip=zeros(1,3)-10;

masapropfreq=zeros(1,5);
masapropend=zeros(1,5);


mastimesaftershort=zeros(1,10);
check007=0;
counteraftershort=0;



timpak=zeros(100,10);


fnames=fopen('names_new.txt','w');

fnamesh=fopen('names_new_high.txt','w');



for u=1:jtrue-1
    strtest=char(array8(u).sstype);
    curstr=char(array8(u).name);
    
    tip=-1;
    for scout=1:length(curstr)-2;
        curtrip(1)=curstr(scout);
        curtrip(2)=curstr(scout+1);
        curtrip(3)=curstr(scout+2);
        
        if (sum (curtrip==adp)==3)
            tip=0;
            countadp=countadp+1;
        end;
        
          if (sum (curtrip==thrombin)==3)
            tip=1;
            countthr=countthr+1;
        end;
        
        
        
    end;
    
    if tip==1
        
        for scout=1:length(curstr)-2;
        curtrip(1)=curstr(scout);
        curtrip(2)=curstr(scout+1);
        curtrip(3)=curstr(scout+2);
        
        if (sum (curtrip==highconc)==3)
            tip=2;
            counthigh=counthigh+1;
        end;
        end;
    end;
    
        if tip==2
        fprintf (fnamesh,'%s \n',curstr);
    end;
       
    if tip==1
        fprintf (fnames,'%s \n',curstr);
    end;
    
    if  (array8(u).fps==2) && (array8(u).datatype==1) % (tip==str2num(tip_for_Taya)) && (array8(u).freq<=5) % && (tip==str2num(tip_for_Taya))   % && (array8(u).datatype==1) && (length(array8(u).sstype)==2) && (sum(array8(u).sstype==nopestr)==2)       (array8(u).freq>=3) && (array8(u).freq<=10) && (array8(u).datatype==1)  &&
        
  %       if (length(strtest)==2) %//(array8(u).datatype==1) && (array8(u).freq>=5)
%        
       %  if (strtest==strno)
             

% 70 20
       k=k+1;
       masapropfreq(1,k)=array8(u).freq;
       mastip(1,k)=tip;
       masapropend(1,k)=array8(u).end;
     
tim1=0;
tim1=array8(u).caspikes(:,2);




% 
% f=figure;
% figure(f);
% plot (tim1);

nmx=round(array8(u).fps*array8(u).end);

tim2=zeros(1,nmx);
tim3=zeros(1,nmx);

p4k=0;

check007=0;

countp4k=0;

for i=1:length(tim1)-1
    if tim1(i)>0
        
      
            
    tim2(1,round(array8(u).fps*tim1(i)))=1;
    
    tim3(1,i)=tim1(i+1)-tim1(i);
    
      if (check007==1)
          counteraftershort=counteraftershort+1;  
          mastimesaftershort(1,counteraftershort)=tim3(1,i);
            
      end;
      
    
    if (tim3(1,i)<=maxtime2)
        check007=1;
    else
        check007=0;
    end;
    
    
    
    if (tim3(1,i)<=maxtime)                   % !!!!!!!!!!!!!!!!!!!!!!
        p4k=p4k+1;
        
        if (tim3(1,i)<=mintime) && (i+2<=length(tim1))
            cntshort=cntshort+1;
            
            statshort(1,cntshort)=tim1(i+2)-tim1(i+1);
        end;
        
        
    else
        
        if (p4k>=0)
        
            p4kcount=p4kcount+1;
            masp4k(1,p4kcount)=p4k;
        
            
            if p4k>3
                for newi=1+timeinp4kcount:1+timeinp4kcount+p4k-1
                    timeinp4k(1,newi)=tim3(1,i-(newi-timeinp4kcount));
                end;
                timeinp4kcount=timeinp4kcount+p4k;
            end;
                
                
            if (p4k==5)
                countp4k=countp4k+1;
                timpak(k,countp4k)=tim1(i);
            end;
            
            p4k=0;
            
            
        end;
    end;
    
    end;
end;
% 

yy=yy+histc(tim3,zz);
yfrq=yfrq+histc(tim3.^-1,zz77);



%plot (zz,yy);


polokna=round(poloknasize*array8(u).fps);          % !!!!!!10 sec!!!!!!!!!!!!!!!!!!!!!!!!







locfreq=zeros(1,nmx-2*polokna);
              % !!!!!!!!!!!!!!!!!!!!!!

for i=polokna+1:nmx-polokna
    s=0;
    for j=i-polokna:i+polokna
        s=s+tim2(1,j);
    end;
    locfreq(1,i-polokna)=s;
    locfreqsum(k,i-polokna)=s;
end;


% 
% 
% f=figure;
% figure(f);
% scur=['this is ',array8(u).name];
% set (f,'name',scur);
% a=axes;
% axes(a);
% set (a,'nextplot','add');
% plot (locfreq);

freqdistrib=freqdistrib+histc(locfreq,z7);

secr=zeros(1,nmx);

array8(u).data(1,4);

if (array8(u).datatype==1)
    
    if (array8(u).data(1,4)>0)
    
         kkk=kkk+1;
        secr (1,round(array8(u).fps*array8(u).data(1,4)))=10;

avcadens=0;
avcadens_shorten=0;
avcadens_spiky=0;

counterspec=0;

    for i=polokna+1:nmx-polokna
        avcadens=avcadens+locfreq(1,i-polokna);
        if (i<=round(array8(u).fps*array8(u).data(1,4))-polokna)
        avcadens_shorten=avcadens_shorten+locfreq(1,i-polokna);
        end;
        
        if (tim2(1,i)>0) && (i-2*polokna>0)
            avcadens_spiky=avcadens_spiky+locfreq(1,i-2*polokna);
            counterspec=counterspec+1;
        end;
    end;
    avcadens=avcadens/(nmx-(2*polokna));
    
    avcadens_shorten=avcadens_shorten/(array8(u).fps*array8(u).data(1,4)-(2*polokna));
    
    if (counterspec>0)
    avcadens_spiky=avcadens_spiky/counterspec;
    end;
    
   
    
    
    
    if ((round(array8(u).fps*array8(u).data(1,4)))-2*polokna > 0) &&  ( round(array8(u).fps*array8(u).data(1,4))-2*polokna <= nmx-2*polokna)  
        
         attempts=0;
    
    atsecr=locfreq(1,(round(array8(u).fps*array8(u).data(1,4)))-2*polokna);
    
    for i=polokna+1:nmx-polokna
        if (i<round(array8(u).fps*array8(u).data(1,4))-polokna) && (locfreq(1,i-polokna)>=atsecr)
        attempts=attempts+1;
        end;
    end;
        
        
        
    fprintf (f17,'%s %g %g %g %g %g \n',array8(u).name, avcadens,avcadens_shorten,avcadens_spiky, locfreq(1,(round(array8(u).fps*array8(u).data(1,4)))-2*polokna),attempts);
    end;
    
    
    
    [hei,wid]=size(array8(u).caspikes);
    
    cad=array8(u).caspikes;
    
    kbefore=0;
    ampbefore=0;
    
    kafter=0;
    ampafter=0;
    
    
    ampbefsecr=0;
    
    chkgranule=0;
    isecr=0;
    isecrafter=0;
    istart=0;
    
    chkstart=0;
    
    intervals=0;
    
    for i=1:hei
        
    if (cad(i,2)<0)
    
    kbefore=kbefore+1;
    ampbefore=ampbefore+cad(i,3);
    end;
    
    if (cad(i,2)>0)
        kafter=kafter+1;
        if (i<hei)
             intervals=intervals+cad(i+1,2)-cad(i,2);
        end;
        
        if (chkstart==0)
        istart=i;
        end;
        
        ampafter=ampafter+cad(i,3);
        chkstart=1;
    end;
    
    if (cad(i,2)>array8(u).data(1,4)) && (chkgranule==0)
        chkgranule=1;
        isecr=i-1;
        isecrafter=kafter-1;
    end;
    
    end;
    
    if (kbefore>0)
        ampbefore=ampbefore/kbefore;
    end;
     if (kafter>0)
        ampafter=ampafter/kafter;
    end;
    
    
    for i=istart:isecr
        
    ampbefsecr=ampbefsecr+cad(i,3);
    end;
    
    if (isecr-istart+1>0)
    ampbefsecr=ampbefsecr/(isecr-istart+1);
    end;
    
   intervals=intervals/(kafter-1);
   
    if (isecr-1>0) && (isecr+1<=hei) && (array8(u).data(1,4)>0)
    fprintf (f22,'%s %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n', array8(u).name, kbefore, kafter, isecrafter,cad(isecr,2),array8(u).data(1,4),ampbefore,ampbefsecr, ampafter,cad(isecr-1,3),cad(isecr,3),cad(isecr+1,3), cad(isecr-1,2),cad(isecr,2),cad(isecr+1,2),intervals);
    end;
end;
end;


% h=plot (secr);
% set (h,'color','red');

% scur=[pwd,'\peak_density_data_new_okno_10_seconds\',array8(u).name,'.tif'];
% saveas (f,scur);

   %  end;
     end;
end;


f=figure;
figure(f);

countadp

countthr


'number of plts that satisfy the criterion:'

k

'number of plts that secreted in definite time among those who satisfy the criterion'

kkk

'percentage:'

100*kkk/k


%jtrue-1


fclose(fnames);
fclose(fnamesh);

numgoodplts=k;

plot (sum(locfreqsum)/k);

nmxmin=800;
locfreqmas=zeros(k,nmxmin);

for i=1:nmxmin
    for j=1:k
        locfreqmas(j,i)=locfreqsum(j,i);
    end;
end;

dist=zeros(k,k);

for i=1:k-1
    for j=i+1:k
        dist(i,j)=(sum((locfreqsum(j,:)-locfreqsum(i,:)).^2))^0.5;
        dist(j,i)=dist(i,j);
    end;
end;




f=figure;
figure(f);

imagesc (dist);

colormap (gray);
        
    [reddim,eig] = cmdscale(dist);
    
    eig;
    
    f=figure;
    figure;
    a=axes;
    axes(a);
    scatter (reddim(:,1),reddim(:,2));

    set (a,'nextplot','add');
    for i=1:k
        if mastip(1,i)==1
            h=scatter (reddim(i,1),reddim(i,2),'fill');
           % set (h,'markersize',3);
        end;
    end;


f=figure;
figure(f);

z90=0:20;
y90=histc(locfreqsum(:,1)',z90);
plot (z90,y90);


% 
% 
% f=figure;
% figure(f);
% 
% z90=0:20;
% y90=histc(locfreqsum(:,98)',z90);
% plot (z90,y90);
% 
% 
% f=figure;
% figure(f);
% 
% z90=0:20;
% y90=histc(locfreqsum(:,393)',z90);
% plot (z90,y90);
% 





%k

frq=yy.^-1;
% 
% f=figure;
% figure(f);
% 
% plot (zz77,yfrq);
% 


yy0=yy/sum(yy);


% j=jtrue-1
% array8(j).name
% array8(j).shear
% array8(j).datatype
% array8(j).data(1,5)

fclose (f17);
fclose (f22);

f17=fopen ('pa4ki.txt','w');

for i=1:length(masp4k)
    fprintf (f17,'%g \n',masp4k (1,i) );
end;

fclose (f17);
% 
zz=0:20;

yy1=histc(masp4k,zz);


yy1=yy1/sum(yy1);


f=figure;
figure(f);




nboot=10;            % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

N=nboot*numgoodplts*100;    

timess=zeros(1,N);
timess2=zeros(1,N);
k=0;
newk=0;

newi=0;

for k=1:numgoodplts
    for u=1:nboot
        
         for i=1:round(masapropend(1,k)*2)
             slu4=rand(1)*100;
             newi=newi+1;
             if (slu4<0.5*masapropfreq(1,k)*100/60) % !!!!!!!!!!!!!!!!!!!!!!!1
                  newk=newk+1;
                     timess(1,newk)=newi*0.5;
                     timess2(1,newi)=1;
             else
                 timess2(1,newi)=0;
             end;
         end;
    end;
end;

N=newi


k=newk;

'number of spikes in simulation'

newk

'expected number of spikes'

testk=0;

for u=1:numgoodplts
    testk=testk + (masapropfreq(1,u)*masapropend(1,u)/60);
end;

testk=testk*nboot;

testk



polokna2=poloknasize*2;          % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ( 10 seconds!!! )

locfreq2=zeros(1,N);

for i=polokna2+1:N-polokna2
    s=0;
    for j=i-polokna2:i+polokna2
        s=s+timess2(1,j);
    end;
    locfreq2(1,i)=s;
end;




p4k=0;
p4kcount=0;
masp4k=zeros(1,1000);
mastimess=zeros(1,k-1);
for i=1:k-1
    tumak=timess(1,i+1)-timess(1,i);
    mastimess(1,i)=tumak;
    
if (tumak<maxtime)           % !!!!!!!!!!!!!!!!!!!!!!!!!!!
    p4k=p4k+1;
else
    if (p4k>=0)
    p4kcount=p4kcount+1;
    masp4k(1,p4kcount)=p4k;
    p4k=0;
    end;
    
end;
end;


zz=0:20;
f=figure;
figure(f);
a=axes;
axes(a);

yy2=histc(masp4k,zz);
set (a,'nextplot','add');



yy2=yy2/sum(yy2);
plot (zz,yy2);

h=plot (zz,yy1);
set (h,'color',[1 0 0]);


zz=0.5:0.5:120;

yy=histc (mastimess,zz);

yy999=histc( mastimesaftershort,zz);

yy999=yy999/sum(yy999);



yy99=histc (mastimess.^-1,zz77);

f=figure;
figure(f);

plot (zz77,yy99);

f=figure;
figure(f);
a=axes;
axes(a);

yy5=yy/sum(yy);

plot (zz,yy5);

set (a,'nextplot','add');

h=plot (zz,yy0);
set (h,'color',[1 0 0]);

h=plot (zz,yy999);

set (h,'color',[1 0 1]);

ftaya=fopen(['intervals_distrib_' tip_for_Taya '.txt'],'w');

for i=1:length(zz)
    fprintf (ftaya,'%g %g %g %g \n',zz(1,i),yy0(1,i),yy999(1,i),yy5(1,i));
end;
fclose(ftaya);



f=figure;
figure(f);
z=1:0.5:5;
y7=histc (timeinp4k,z);
plot (z,y7);


f=figure;
figure(f);
a=axes;
axes(a);
set (a,'nextplot','add');
h=plot (z7,freqdistrib/sum(freqdistrib));
set (h,'color',[1 0 0]);

y8=histc(locfreq2,z7);
plot (z7, y8/sum(y8));

y8=y8/sum(y8);

freqdistrib=freqdistrib/sum(freqdistrib);





ftaya=fopen(['peak_density_distrib_tip_' tip_for_Taya '_okno_' num2str(2*poloknasize) '.txt'],'w');
for i=1:length(z7)
    fprintf (ftaya,'%g %g %g \n',z7(1,i),freqdistrib(i),y8(1,i));
end;
fclose(ftaya);


% f20=fopen('peak_density_data_calcium_preceding_peak_time.txt','w');
% 
% kkk=0;
% 
% for u=1:jtrue-1
%     
%      if (array8(u).datatype==1)
%        
%        k=k+1;
%      
% tim1=0;
% tim1=array8(u).caspikes(:,2);
% array8(u).name
% 
% % f=figure;
% % figure(f);
% % plot (tim1);
% 
% nmx=1200;
% 
% tim2=zeros(1,nmx);
% for i=1:length(tim1)
%     if tim1(i)>0
%     tim2(1,2*tim1(i))=1;
%     end;
% end;
% % 
% % plot (tim2);
% 
% 
% locfreq=zeros(1,nmx);
% polokna=10;
% 
% for i=polokna+1:nmx-polokna
%     s=0;
%     for j=i-polokna:i+polokna
%         s=s+tim2(1,j);
%     end;
%     locfreq(1,i)=s;
% end;
% 
% f=figure;
% figure(f);
% scur=['this is ',array8(u).name];
% set (f,'name',scur);
% a=axes;
% axes(a);
% set (a,'nextplot','add');
% plot (locfreq);
% 
% 
% secr=zeros(1,nmx);
% 
% array8(u).data(1,20)
% 
% 
% if (array8(u).data(1,20)>0)
%     
%     kkk=kkk+1;
% secr (1,2*array8(u).data(1,20))=10;
% 
% avcadens=0;
% avcadens_shorten=0;
% 
%     for i=1:1000
%         avcadens=avcadens+locfreq(1,i);
%         if (i<=2*array8(u).data(1,20))
%         avcadens_shorten=avcadens_shorten+locfreq(1,i);
%         end;
%     end;
%     avcadens=avcadens/1000;
%     
%     avcadens_shorten=avcadens_shorten/(2*array8(u).data(1,20));
%     
%     
%     fprintf (f20,'%s %g %g %g \n',array8(u).name, avcadens,avcadens_shorten, locfreq(1,2*array8(u).data(1,20) ));
%     
% end;
% h=plot (secr);
% set (h,'color','red');
% 
% scur=[pwd,'\peak density calcium peak\',array8(u).name,'.tif'];
% saveas (f,scur);
% 
%      end;
% end;
% fclose (f20);



clear all;
